<template>
  <div>
    <!-- 添加弹出框 -->
    <el-dialog
      :title="dialogFormTitle"
      :visible.sync="dialogFormVisible"
      @close="closedialog"
    >
      <el-form
        ref="produceForm"
        :rules="rules"
        :model="productionordermodify"
        label-width="80px"
        style="width: 300px"
      >
        <el-form-item label="产品名称" prop="pname">
          <label v-if="isEdit">{{ productionordermodify.pname }}</label>
          <el-select
            v-model="productionordermodify.pname"
            placeholder="请选择产品"
            clearable
            :disabled="isEdit"
            v-if="!isEdit"
          >
            <el-option
              v-for="item in goods"
              :key="item.value"
              :label="item.gname"
              :value="item.gname"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="生产数量" prop="pnum">
          <label v-if="isEdit">{{ productionordermodify.pnum }}</label>
          <el-input
            v-model="productionordermodify.pnum"
            clearable
            :disabled="isEdit"
            v-if="!isEdit"
          ></el-input>
        </el-form-item>

        <el-form-item label="产品单位" prop="punit">
          <label v-if="isEdit">{{ productionordermodify.punit }}</label>
          <el-select
            v-model="productionordermodify.punit"
            placeholder="请选择单位"
            :disabled="isEdit"
            v-if="!isEdit"
          >
            <el-option label="千克" value="千克"></el-option>
            <el-option label="吨" value="吨"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="下单日期" prop="stime">
          <label v-if="isEdit">{{ productionordermodify.stime }}</label>
          <el-input
            type="date"
            v-model="productionordermodify.stime"
            :disabled="true"
            v-if="!isEdit"
          ></el-input>
        </el-form-item>

        <el-form-item label="预计完工" prop="etime">
          <label v-if="isEdit">{{ productionordermodify.etime }}</label>
          <el-input
            type="date"
            v-model="productionordermodify.etime"
            :disabled="isEdit"
            v-if="!isEdit"
          ></el-input>
        </el-form-item>

        <el-form-item label="生产部门" prop="department.did">
          <label v-if="isEdit">{{
            productionordermodify.department.dname
          }}</label>
          <el-select
            v-model="productionordermodify.department.did"
            placeholder="请选择部门"
            clearable
            :disabled="isEdit"
            v-if="!isEdit"
          >
            <el-option
              v-for="item in departments"
              :key="item.value"
              :label="item.dname"
              :value="item.did"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm">取 消</el-button>
        <el-button type="primary" @click="handEditAdd">确定</el-button>
      </div>
    </el-dialog>

    <!-- 搜索表单 -->
    <el-form
      ref="productionorder"
      :model="productionorder"
      label-width="80px"
      :inline="true"
    >
      <el-form-item>
        <el-button type="primary" @click="batchDelete">批量删除</el-button>
      </el-form-item>

      <el-form-item label="生产单号">
        <el-input v-model="productionorder.poid" clearable></el-input>
      </el-form-item>

      <el-form-item label="产品名称">
        <el-input v-model="productionorder.pname" clearable></el-input>
      </el-form-item>

      <el-form-item label="生产状态" style="width: 120px">
        <el-select v-model="productionorder.pstate" placeholder="请选择状态">
          <el-option label="待生产" value="待生产"></el-option>
          <el-option label="生产中" value="生产中"></el-option>
          <el-option label="暂停中" value="暂停中"></el-option>
          <el-option label="已结束" value="已结束"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="下单日期">
        <el-input type="date" v-model="productionorder.stime"></el-input>
      </el-form-item>

      <el-form-item label="生产部门">
        <el-select
          v-model="productionorder.department.did"
          placeholder="请选择部门"
          clearable
        >
          <el-option
            v-for="item in departments"
            :key="item.value"
            :label="item.dname"
            :value="item.did"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询信息</el-button>
        <el-button @click="handAdd">创建生产</el-button>
        <el-button @click="clearAllFields">清空</el-button>
      </el-form-item>
    </el-form>

    <!-- 展示数据 -->
    <el-table
      :data="pageBean.beanList"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="poid" label="生产单号" width="140" align="center">
      </el-table-column>
      <el-table-column prop="pname" label="产品名称" width="100" align="center">
      </el-table-column>
      <el-table-column prop="yield" label="预计产量" width="100" align="center">
      </el-table-column>
      <el-table-column prop="stime" label="下单日期" width="140" align="center">
      </el-table-column>
      <el-table-column
        prop="etime"
        label="预计完工日期"
        width="140"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="department.dname"
        label="生产部门"
        width="140"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="pstate"
        label="生产状态"
        width="140"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag
            :type="
              scope.row.pstate === '生产中'
                ? 'success'
                : scope.row.pstate === '待生产'
                ? 'danger'
                : scope.row.pstate === '暂停中'
                ? 'info'
                : 'primary'
            "
            disable-transitions
            >{{ scope.row.pstate }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 展示页码 -->
    <div class="block">
      <span class="demonstration"></span>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[1, 3, 6]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageBean.totalRecord"
      >
      </el-pagination>
    </div>
  </div>
</template>
  
  <script>
import axios from "axios";

export default {
  data() {
    var checkenum = (rule, value, callback) => {
      if (value === undefined || value === "") {
        callback(new Error("填写数量"));
      } else if (!/^[1-9]\d*$/.test(value)) {
        callback(new Error("数量格式错误"));
      } else {
        callback();
      }
    };
    var checketime = (rule, value, callback) => {
      if (value === undefined || value === "") {
        callback(new Error("结束时间不能为空"));
      } else if (new Date(value) < new Date(this.productionordermodify.stime)) {
        callback(new Error("结束时间不能早于开始时间"));
      } else {
        callback();
      }
    };

    return {
      isEdit: false,
      produceForm: null,
      dialogFormVisible: false,
      multipleSelection: [],
      dialogFormTitle: "",
      pageIndex: 1,
      pageSize: 6,
      pageBean: {},
      departments: {},
      goods: {},
      rules: {
        pname: [
          { required: true, message: "请输入产品名称", trigger: "change" },
        ],
        punit: [
          { required: true, message: "请选择产品单位", trigger: "change" },
        ],
        pnum: [
          { required: true, message: "请输入产品数量", trigger: "blur" },
          { validator: checkenum, trigger: "blur" },
        ],
        etime: [
          {
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
          { validator: checketime, trigger: "change" },
        ],
        "department.did": [
          { required: true, message: "请选择部门", trigger: "change" },
        ],
      },

      productionorder: {
        department: {
          did: null,
        },
      },
      productionordermodify: {
        department: {
          did: null,
        },
      },
    };
  },
  mounted() {
    this.getAll();
    this.getAllDepart();
    this.getAllGoods();
  },
  methods: {
    //  获取当前时间
    currentDate() {
      return (
        new Date().getFullYear() +
        "-" +
        ("0" + (new Date().getMonth() + 1)).slice(-2) +
        "-" +
        ("0" + new Date().getDate()).slice(-2)
      );
    },
    // 查询所有信息
    getAll() {
      axios
        .post(
          "/produced/findAll/" + this.pageIndex + "/" + this.pageSize,
          this.productionorder
        )
        .then((resp) => {
          if (resp.data.code == 200) {
            this.pageBean = resp.data.data;
          }
        });
    },
    // 查询所有部门
    getAllDepart() {
      axios.post("/produced/findAllDept").then((resp) => {
        this.departments = resp.data.data;
      });
    },
    // 查询所有产品
    getAllGoods() {
      axios.post("/produced/findAllGoods").then((resp) => {
        this.goods = resp.data.data;
      });
    },
    //  查询按钮
    onSubmit() {
      this.pageIndex = 1;
      this.pageSize = 6;
      this.getAll();
    },
    //  重置搜索
    clearAllFields() {
      this.productionorder = {
        poid: "",
        pname: "",
        stime: "",
        department: { did: "" },
      };
      this.getAll();
    },
    //  添加弹出框
    handAdd() {
      (this.dialogFormVisible = true),
        (this.dialogFormTitle = "创建生产单"),
        (this.productionordermodify.stime = this.currentDate());
    },
    //  添加生产单
    handEditAdd() {
      this.$refs.produceForm.validate((valid) => {
        if (valid) {
          if (this.dialogFormTitle == "创建生产单") {
            axios
              .post("/produced/add", this.productionordermodify)
              .then((resp) => {
                if (resp.data.code == 200) {
                  this.$message("创建成功");
                  this.getAll();
                } else {
                  this.$message.error(JSON.stringify(resp.data.msg));
                }
                this.resetForm();
                this.dialogFormVisible = false;
              });
          } else {
            axios
              .post("/produced/modify", this.productionordermodify)
              .then((resp) => {
                if (resp.data.code == 200) {
                  this.$message("修改成功");
                  this.getAll();
                } else {
                  this.$message.error(JSON.stringify(resp.data.msg));
                }
                this.dialogFormVisible = false;
              });
          }
        } else {
          this.$message("验证失败");
        }
      });
    },
    //  编辑生产单
    handEdit(index, row) {
      this.dialogFormVisible = true;
      this.dialogFormTitle = "修改生产单";
      this.productionordermodify = { ...row };
      // 生产中和结束的生产单不可修改
      this.isEdit = this.productionordermodify.pstate != "待生产";
    },
    //  清空表单
    resetForm() {
      this.$refs.produceForm.resetFields();
      this.dialogFormVisible = false
    },
    //  关闭弹出框
    closedialog() {
      //  情况数据
      this.resetForm();
      this.isEdit = false;
    },
    //  复选框选中
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      //  重新查询
      this.pageSize = val;
      this.getAll();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      //  重新查询
      this.pageIndex = val;
      this.getAll();
    },
    //  删除
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          axios.post("/produced/delete", row).then((resp) => {
            if (resp.data.code == 200) {
              this.$message("删除成功");
              this.getAll();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //  批量删除
    batchDelete(index, row) {
      if (this.multipleSelection.length > 0) {
        let myids = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          myids.push(this.multipleSelection[i].id);
        }
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            axios
              .post("/produced/deletebatchget", { ids: myids })
              .then((resp) => {
                if (resp.data.code == 200) {
                  this.$message("删除成功");
                  this.getAll();
                } else {
                  this.$message.error("删除失败");
                }
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      } else {
        this.$message.error("请选择要删除的数据");
      }
    },
  },
};
</script>
  
  <style>
</style>